package DAO;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class ConnectManager {
    private static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
//    private static final String DATABASE_URL = "jdbc:mysql://114.132.156.199:3306/21201701";
//    private static final String USER = "21201701";

    private static final String DATABASE_URL = "jdbc:mysql://localhost:3306/web";
    private static final String USER = "root";
    private static final String PASSWORD = "123456";

    public static Connection getConnection() {
        try {
            Class.forName(JDBC_DRIVER);
            Connection conn = java.sql.DriverManager.getConnection(DATABASE_URL, USER, PASSWORD);
            System.out.println("Connection Successful!");
            return conn;
        } catch (SQLException e) {
            System.out.println("Connection Failed! Check output console");
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            System.out.println("Connection Failed! Check output console");
            throw new RuntimeException(e);
        }
    }

    public static void closeConnection(Connection conn, Statement stmt, ResultSet rst){
        try {
            if (conn != null)
                conn.close();
            if (stmt != null)
                stmt.close();
            if (rst != null)
                rst.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
